//
//  QMessageListVC.m
//  QoilPublic
//
//  Created by 杨凯 on 2017/7/12.
//  Copyright © 2017年 杨凯. All rights reserved.
//

#import "QMessageListVC.h"
#import "QMessageCell.h"
#import "QMsgListModel.h"
#import "MJExtension.h"
#import "QMessageListCell.h"
#import "QActivityWebVC.h"
#import "QMsgDetailVC.h"
@interface QMessageListVC ()<UITableViewDataSource,UITableViewDelegate>
{
    NSInteger _index;
    NSMutableArray *_dataArr;
    NSMutableArray * _puidArr;
}
@property (nonatomic,strong)UITableView *tbView;
@end

@implementation QMessageListVC

- (id)init{
    self = [super init];
    if (self) {
        
        self.hidesBottomBarWhenPushed = YES;
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createUI];
    
    _dataArr = [NSMutableArray array];
    _puidArr = [NSMutableArray array];
    
    [self requestData];
    
    _tbView.mj_header = [QViewUtility showHeadRefresh:^{
        [_tbView.mj_footer resetNoMoreData];
        [self requestData];
    }];
    
    _tbView.mj_footer = [QViewUtility showFootRefresh:^{
        [self requestMoreData];
    }];
    
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
}

- (void)requestData{
    WS(wSelf);
    _index = 0;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMsgList"];
    NSDictionary * para = [QParamService getMsgListWithMac:[QParamService getUUID] PtTypeh:self.ptType Start:start PageSize:KpageSize];
    
    [QViewUtility showMBHudToView:self.view hideBg:YES];
    [QViewUtility disMissNoRecordInVC:self];
    [QViewUtility disMissNoNetWorkInVC:self];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_header endRefreshing];
        [QViewUtility hideMBHudToView:self.view];
        if(error){
            if([QUserInfo sharedInfo].isHaveNetWork == NO){
                [QViewUtility showNoNetWorkInView:_tbView];
            }
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            [_dataArr removeAllObjects];
            
            
            _dataArr = [QMsgListModel mj_objectArrayWithKeyValuesArray:[basModel.items objectForKey:@"list"]];
            
            if(_dataArr.count == 0){
                [QViewUtility showNoRecordViewInView:_tbView Title:@"没有任何消息哦~"];
            }else{
                if(_dataArr.count < 10){
                    [_tbView.mj_footer noticeNoMoreData];
                }
                if (_dataArr.count) {
                    [wSelf requestIsLook];//标记已读
                }
                
            }
            [_tbView reloadData];
            
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
}

- (void)requestMoreData{
    WS(wSelf);
    _index++;
    NSString *start = [NSString stringWithFormat:@"%ld",(long)_index];
    NSString *url = [QParamService urlWithAction:@"getMsgList"];
    NSDictionary * para = [QParamService getMsgListWithMac:[QParamService getUUID] PtTypeh:self.ptType Start:start PageSize:KpageSize];
    [[QHttpManager sharedManager] POST:url parameterMD5:para completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        [_tbView.mj_footer endRefreshing];
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        
        if([basModel.result isEqualToString:@"success"]){
            NSArray * arr = [QMsgListModel mj_objectArrayWithKeyValuesArray:[basModel.items objectForKey:@"list"]];
            [_dataArr addObjectsFromArray:arr];
            if(arr.count < 10){
                [_tbView.mj_footer noticeNoMoreData];
            }
            if (_dataArr.count) {
                [wSelf requestIsLook];//标记已读
            }
            [_tbView reloadData];
        }else{
            [QViewUtility showAlertViewWithImage:[UIImage imageNamed:@"cry_image"] content:basModel.errMsg btnTitle:showAlertBtnTitle type:1 myBlock:^{}];
        }
    }];
    
}
//标记为已读
- (void)requestIsLook{
    for (QMsgListModel * model in _dataArr) {
        [_puidArr addObject:model.purId];
    }
    WS(wSelf);
    NSString *purIds = [_puidArr componentsJoinedByString:@","];
    NSString *url = [QParamService urlWithAction:@"addPushIsLook"];
    NSDictionary *param = [QParamService addPushIsLookWithPurId:purIds];
    [[QHttpManager sharedManager] POST:url parameterMD5:param completion:^(NSData *data, NSDictionary *dict, NSError *error) {
        if(error){
            return ;
        }
        QBaseModel *basModel = [QBaseModel objectFromDict:dict];
        if([basModel.result isEqualToString:@"success"]){
            CLog(@"标记为已读");
            if (wSelf.block) {
                wSelf.block();
            }
        }else{
        }
    }];
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc]init];
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 85;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return _dataArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([self.ptType isEqualToString:@"3"]) {
       QMessageCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QMessageCell" forIndexPath:indexPath];
        cell.listModel = [_dataArr objectAtIndex:indexPath.section];
        return cell;
        
    }else{
        QMessageListCell * cell = [tableView dequeueReusableCellWithIdentifier:@"QMessageListCell" forIndexPath:indexPath];
        cell.listModel = [_dataArr objectAtIndex:indexPath.section];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if ([self.ptType isEqualToString:@"1"]) {
        
    }
    QMsgListModel *listModel = [_dataArr objectAtIndex:indexPath.section];
    if ([listModel.ptId isEqualToString:@"1"]) {
        self.tabBarController.selectedIndex=1;
    }else if ([listModel.ptId isEqualToString:@"2"]){
        
        QMsgDetailVC * vc = [[QMsgDetailVC alloc] init];
        vc.listModel = [_dataArr objectAtIndex:indexPath.section];
        [self.navigationController pushViewController:vc animated:YES];
    }else if ([listModel.ptId isEqualToString:@"3"]){
        
        QActivityWebVC * vc = [[QActivityWebVC alloc] init];
        vc.url = listModel.openUrl;
        vc.listModel = [_dataArr objectAtIndex:indexPath.section];
        [self.navigationController pushViewController:vc animated:YES];
    }
}


- (void)createUI{
    [self.view addSubview:self.tbView];
    self.tbView.frame = CGRectMake(12, 0, kScreenWidth - 24, kScreenHeight );
    [self.tbView registerNib:[UINib nibWithNibName:@"QMessageCell" bundle:nil] forCellReuseIdentifier:@"QMessageCell"];
    
    [self.tbView registerNib:[UINib nibWithNibName:@"QMessageListCell" bundle:nil] forCellReuseIdentifier:@"QMessageListCell"];
    
    
}
#pragma mark - Getter/Setter
- (UITableView*)tbView {
    if (!_tbView) {
        _tbView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tbView.dataSource = self;
        _tbView.delegate = self;
        _tbView.backgroundColor = [UIColor clearColor];
        _tbView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tbView;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
